package labox.innovation.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import labox.innovation.gameserver.util.FloodProtectorConfig;
import labox.innovation.gameserver.util.StringUtil;
import labox.innovation.util.L2Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FloodprotectorConfig {
    private static final Logger _log = LoggerFactory.getLogger(FloodprotectorConfig.class.getName());

    private static final String FLOOD_PROTECTOR_FILE = "./config/floodprotector.properties";

    // --------------------------------------------------
    // FloodProtector Settings
    // --------------------------------------------------
    public static FloodProtectorConfig FLOOD_PROTECTOR_USE_ITEM;

    public static FloodProtectorConfig FLOOD_PROTECTOR_ROLL_DICE;

    public static FloodProtectorConfig FLOOD_PROTECTOR_FIREWORK;

    public static FloodProtectorConfig FLOOD_PROTECTOR_ITEM_PET_SUMMON;

    public static FloodProtectorConfig FLOOD_PROTECTOR_HERO_VOICE;

    public static FloodProtectorConfig FLOOD_PROTECTOR_GLOBAL_CHAT;

    public static FloodProtectorConfig FLOOD_PROTECTOR_SUBCLASS;

    public static FloodProtectorConfig FLOOD_PROTECTOR_DROP_ITEM;

    public static FloodProtectorConfig FLOOD_PROTECTOR_SERVER_BYPASS;

    public static FloodProtectorConfig FLOOD_PROTECTOR_MULTISELL;

    public static FloodProtectorConfig FLOOD_PROTECTOR_TRANSACTION;

    public static void load() {
        _log.info("Loading floodprotector Configuration Files...", Config.SERVER_ID);
        InputStream is = null;
        try {

            FLOOD_PROTECTOR_USE_ITEM = new FloodProtectorConfig("UseItemFloodProtector");
            FLOOD_PROTECTOR_ROLL_DICE = new FloodProtectorConfig("RollDiceFloodProtector");
            FLOOD_PROTECTOR_FIREWORK = new FloodProtectorConfig("FireworkFloodProtector");
            FLOOD_PROTECTOR_ITEM_PET_SUMMON = new FloodProtectorConfig("ItemPetSummonFloodProtector");
            FLOOD_PROTECTOR_HERO_VOICE = new FloodProtectorConfig("HeroVoiceFloodProtector");
            FLOOD_PROTECTOR_GLOBAL_CHAT = new FloodProtectorConfig("GlobalChatFloodProtector");
            FLOOD_PROTECTOR_SUBCLASS = new FloodProtectorConfig("SubclassFloodProtector");
            FLOOD_PROTECTOR_DROP_ITEM = new FloodProtectorConfig("DropItemFloodProtector");
            FLOOD_PROTECTOR_SERVER_BYPASS = new FloodProtectorConfig("ServerBypassFloodProtector");
            FLOOD_PROTECTOR_MULTISELL = new FloodProtectorConfig("MultiSellFloodProtector");
            FLOOD_PROTECTOR_TRANSACTION = new FloodProtectorConfig("TransactionFloodProtector");

            L2Properties security = new L2Properties();
            is = new FileInputStream(new File(FLOOD_PROTECTOR_FILE));
            security.load(is);

            loadFloodProtectorConfigs(security);
        } catch (Exception e) {
            _log.error("Failed to Load " + FLOOD_PROTECTOR_FILE + " File.", e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                _log.error("InputStream close occured IOException");
            }
        }
    }

    /**
     * Loads flood protector configurations.
     */
    private static void loadFloodProtectorConfigs(final L2Properties properties) {
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_USE_ITEM, "UseItem", "4");
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_ROLL_DICE, "RollDice", "42");
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_FIREWORK, "Firework", "42");
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_ITEM_PET_SUMMON, "ItemPetSummon", "16");
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_HERO_VOICE, "HeroVoice", "100");
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_GLOBAL_CHAT, "GlobalChat", "5");
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_SUBCLASS, "Subclass", "20");
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_DROP_ITEM, "DropItem", "10");
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_SERVER_BYPASS, "ServerBypass", "5");
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_MULTISELL, "MultiSell", "1");
        loadFloodProtectorConfig(properties, FLOOD_PROTECTOR_TRANSACTION, "Transaction", "10");
    }

    /**
     * Loads single flood protector configuration.
     * 
     * @param properties
     *            L2Properties file reader
     * @param config
     *            flood protector configuration instance
     * @param configString
     *            flood protector configuration string that determines for which
     *            flood protector configuration should be read
     * @param defaultInterval
     *            default flood protector interval
     */
    private static void loadFloodProtectorConfig(final L2Properties properties, final FloodProtectorConfig config, final String configString, final String defaultInterval) {
        config.FLOOD_PROTECTION_INTERVAL = Integer.parseInt(properties.getProperty(StringUtil.concat("FloodProtector", configString, "Interval"), defaultInterval));
        config.LOG_FLOODING = Boolean.parseBoolean(properties.getProperty(StringUtil.concat("FloodProtector", configString, "LogFlooding"), "False"));
        config.PUNISHMENT_LIMIT = Integer.parseInt(properties.getProperty(StringUtil.concat("FloodProtector", configString, "PunishmentLimit"), "0"));
        config.PUNISHMENT_TYPE = properties.getProperty(StringUtil.concat("FloodProtector", configString, "PunishmentType"), "none");
        config.PUNISHMENT_TIME = Integer.parseInt(properties.getProperty(StringUtil.concat("FloodProtector", configString, "PunishmentTime"), "0"));
    }
}
